package com.leaf.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.leaf.core.bean.product.Brand;
import com.leaf.core.bean.product.BrandQuery;
import com.leaf.core.dao.product.BrandDao;
import com.leaf.core.service.BrandService;

import cn.itcast.common.page.Pagination;

@Service("brandService")
@Transactional
public class BrandServiceImpl implements BrandService {

	@Autowired
	private BrandDao brandDao;

	@Override
	public Pagination getPaginationByQuery(String name, Integer isDisplay, Integer pageNo) {

		BrandQuery brandQuery = new BrandQuery();
		brandQuery.setPageNo(Pagination.cpn(pageNo));
		
		StringBuilder sb = new StringBuilder();
		
		if (name != null) {
			brandQuery.setName(name);
			sb.append("name=").append(name);
		}
		
		if (null != isDisplay) {
			brandQuery.setIsDisplay(isDisplay);
			sb.append("isDisplay=").append(isDisplay);
		} else {
			brandQuery.setIsDisplay(1);
			sb.append("isDisplay=").append(1);
		}

		Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(),
				brandDao.findBrandCountByQuery(brandQuery));

		List<Brand> brands = brandDao.findBrandListByQuery(brandQuery);
		
		pagination.setList(brands);
		pagination.pageView("/brand/list", sb.toString());

		return pagination;
	}

}
